package com.xbongbong.paas.help.subform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.help.subform.abstracts.AbstractTransferSubForm;
import com.xbongbong.saas.domain.entity.DistributorAccountEntity;
import com.xbongbong.saas.enums.business.DistributorAccountEnum;
import com.xbongbong.saas.enums.subform.DistributorAccountSubFormEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 魏荣杰
 * @date 2020/8/25 10:35
 * @since
 * @version
 */
@Component
public class DistributorAccountTransferSubForm extends AbstractTransferSubForm {

    private static final Logger LOG = LoggerFactory.getLogger(DistributorAccountTransferSubForm.class);


    public PaasFormDataEntity transferEntityToFormData(DistributorAccountEntity distributorAccountEntity) {
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        paasFormDataEntity.setId(distributorAccountEntity.getId());
        paasFormDataEntity.setCorpid(distributorAccountEntity.getCorpid());
        paasFormDataEntity.setDel(distributorAccountEntity.getDel());
        paasFormDataEntity.setAddTime(distributorAccountEntity.getAddTime());
        paasFormDataEntity.setUpdateTime(distributorAccountEntity.getUpdateTime());
        paasFormDataEntity.setFormId(distributorAccountEntity.getFormId());
        paasFormDataEntity.setCreatorId(distributorAccountEntity.getCreatorId());
        JSONObject dbJson = JSONObject.parseObject(JSON.toJSONString(distributorAccountEntity));
        JSONObject data = new JSONObject();
        DistributorAccountSubFormEnum[] values = DistributorAccountSubFormEnum.values();
        for (DistributorAccountSubFormEnum value : values) {
            String dbAttr = value.getDbAttr();
            String dataAttr = value.getDataAttr();
            Integer fieldType = value.getFieldType();
            formatDataForEs(dbJson, data, dbAttr, dataAttr, fieldType);
        }
        data.put(DistributorAccountEnum.AUTHORITY.getAttr(), data.getString(DistributorAccountEnum.AUTHORITY.getAttr()));
        data.put(DistributorAccountEnum.ACCOUNT_STATUS.getAttr(), data.getString(DistributorAccountEnum.ACCOUNT_STATUS.getAttr()));
        paasFormDataEntity.setData(data);

        return paasFormDataEntity;
    }

    public List<PaasFormDataEntity> transferEntityToFormDataList(List<DistributorAccountEntity> distributorAccountEntities) {
        List<PaasFormDataEntity> paasFormDataEntities = new ArrayList<>();
        for (DistributorAccountEntity distributorAccountEntity : distributorAccountEntities) {
            PaasFormDataEntity paasFormDataEntity = transferEntityToFormData(distributorAccountEntity);
            paasFormDataEntities.add(paasFormDataEntity);
        }
        return paasFormDataEntities;
    }

    public DistributorAccountEntity transferFormDataToEntity(PaasFormDataEntity paasFormDataEntity) {

        JSONObject data = paasFormDataEntity.getData();

        JSONObject dbData = new JSONObject();
        DistributorAccountSubFormEnum[] values = DistributorAccountSubFormEnum.values();
        for (DistributorAccountSubFormEnum distributorAccountSubFormEnum : values) {
            String dbAttr = distributorAccountSubFormEnum.getDbAttr();
            String dataAttr = distributorAccountSubFormEnum.getDataAttr();
            Integer fieldType = distributorAccountSubFormEnum.getFieldType();
            formatDataForDb(data, dbData, dbAttr, dataAttr, fieldType);
        }
        DistributorAccountEntity distributorAccountEntity = JSON.parseObject(dbData.toJSONString(), DistributorAccountEntity.class);
        distributorAccountEntity.setAddTime(paasFormDataEntity.getAddTime());
        distributorAccountEntity.setUpdateTime(paasFormDataEntity.getUpdateTime());
        distributorAccountEntity.setCorpid(paasFormDataEntity.getCorpid());
        distributorAccountEntity.setDel(paasFormDataEntity.getDel());
        distributorAccountEntity.setId(paasFormDataEntity.getId());
        distributorAccountEntity.setFormId(paasFormDataEntity.getFormId());
        distributorAccountEntity.setCreatorId(paasFormDataEntity.getCreatorId());
        return distributorAccountEntity;
    }


    public List<DistributorAccountEntity> transferFormDataToEntityList(List<PaasFormDataEntity> paasFormDataEntities) {
        List<DistributorAccountEntity> distributorAccountEntities = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            DistributorAccountEntity distributorAccountEntity = transferFormDataToEntity(paasFormDataEntity);
            distributorAccountEntities.add(distributorAccountEntity);
        }
        return distributorAccountEntities;

    }
}
